package com.watereast;

import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.lang3.StringUtils;

public class AesUtils {
    public static final String DEFAULT_WORLD = "policycheck2019";

    private static final String KEY_ALGORITHM = "AES";

    private static final String CHARSET = "UTF-8";

    public static final String SXWX_UNDER_WRITING = "sxwx2kjgx";

    public static final String SXWX_UNDER_WRITING_EMP = "sxwx2bdgj";

    public static final String KJGX_DING_DONG_NOTICE = "newtouchnewtouch";

    public static final String PLANBOOK_WORLD = "GapToPlanBook320";

    public static final String KEJIGEXIAN_ANALYSE_KEY = "keJiAnalyseKey001";

    public static String encrypt(String uncryptedStr, String secretKey) {
        try {
            if (StringUtils.isEmpty(uncryptedStr))
                return null;
            Cipher cipher = Cipher.getInstance("AES");
            byte[] byteContent = uncryptedStr.getBytes("UTF-8");
            cipher.init(1, getSecretKey(secretKey));
            byte[] result = cipher.doFinal(byteContent);
            return parseByte2HexStr(result);
        } catch (Exception exception) {
            return null;
        }
    }

    public static String decrypt(String encryptedStr, String secretKey) {
        try {
            if (StringUtils.isEmpty(encryptedStr))
                return null;
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(2, getSecretKey(secretKey));
            byte[] result = cipher.doFinal(parseHexStr2Byte(encryptedStr));
            return new String(result, "UTF-8");
        } catch (Exception exception) {
            return null;
        }
    }

    public static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr == null || hexStr.length() < 1)
            return null;
        hexStr = hexStr.toUpperCase();
        byte[] result = new byte[hexStr.length() / 2];
        char[] hexChars = hexStr.toCharArray();
        for (int i = 0; i < hexStr.length() / 2; i++) {
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
            result[i] = (byte)(high * 16 + low);
        }
        return result;
    }

    private static SecretKeySpec getSecretKey(String pwd) {
        if (StringUtils.isEmpty(pwd))
            pwd = "policycheck2019";
        KeyGenerator kgen = null;
        try {
            kgen = KeyGenerator.getInstance("AES");
            SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
            random.setSeed(pwd.getBytes());
            kgen.init(128, random);
            SecretKey secretKey = kgen.generateKey();
            byte[] enCodeFormat = secretKey.getEncoded();
            SecretKeySpec keySpec = new SecretKeySpec(enCodeFormat, "AES");
            return keySpec;
        } catch (Exception exception) {
            return null;
        }
    }

    public static String parseByte2HexStr(byte[] buf) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < buf.length; i++) {
            String hex = Integer.toHexString(buf[i] & 0xFF);
            if (hex.length() == 1)
                hex = '0' + hex;
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }

    public static String bytes2HexString(byte[] b) {
        if (null == b)
            return null;
        String ret = "";
        for (int i = 0; i < b.length; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1)
                hex = '0' + hex;
            ret = ret + hex.toUpperCase();
        }
        return ret;
    }

    public static void main(String[] args) {
        String data = "A4B7A273F998242319475E19BEE6C34E462A65B772DAA835ECCFD278B76EA693F4C565E06A68146665941A7C5570EFAB0A9A89D776134FA9C3211D9602EC66B22E784BEB82B29068CD6C800F03C44AF2E6C75C03FAE3591A433E45D69FB473DEB12BB79EF6ACC21C24CA7C45B0473072A13015E8EEFC3D9110348FC0D4DEA22128719AC3CA79C3E2302D87CC9EF3E5097BDA18384623F2FCD4979C89A49F6C1A4853ECD46996432668B90406D22CDBAD20F82F89F9AC2DD49B27D6A86265ED8306EBF7148DC4959F7662148483AC9B2D";
        String encryptJson = "{\n    \"cardNo\": \"\",\n    \"birthday\": \"2003-09-17\",\n    \"sex\": \"1\",\n    \"gapValue\": \"200.01\",\n    \"name\": \"\",\n    \"ylxzzAssessUuid\": \"83f8147f22e547e4a3b9b32d71cf639c\", \n\t\"empNo\": \"SHH28106\"}";
        String encryptValue = encrypt(encryptJson, "policycheck2019");
        String decryptData = decrypt(data, "policycheck2019");
        System.out.println("+ encryptValue);
                System.out.println("+ decryptData);
    }
}
